package org.easy.tool.common;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

/**
 * @author Valley
 * Created by Valley on 2023/8/28 15:20
 */
public class ImgUtil {

    /**
     * 功能：将base64字符串转化为图片
     */
    public static void base64ToImg(String base64, String imgPath) throws Exception{
        // 解码Base64字符串为字节数组
        byte[] imageBytes = Base64.getDecoder().decode(base64);
        // 将字节数组转化为BufferedImage
        BufferedImage img = ImageIO.read(new ByteArrayInputStream(imageBytes));
        // 写入到文件，文件路径应替换为你想要的路径
        ImageIO.write(img, "png", FileIOUtil.file(imgPath));
    }

    /**
     * 功能：将图片按照A4纸比例进行切割
     */
    public static void cut2A4(String path, String dest) throws Exception {
        // 读取原始图片
        File file = new File(path);
        BufferedImage originalImage = ImageIO.read(file);
        // 计算A4纸比例对应高度
        int pageHeight = (int) (originalImage.getWidth() / (double) 210 * 297);
        int pageCount = (int) Math.ceil(originalImage.getHeight() / (double) pageHeight);
        // 分页并保存为单独的图像文件
        for (int i = 0; i < pageCount; i++) {
            // 计算当前分页的起始位置
            int startY = i * pageHeight;
            // 创建新的BufferedImage对象来保存当前分页
            BufferedImage pageImage = new BufferedImage(
                    originalImage.getWidth(), pageHeight, BufferedImage.TYPE_INT_RGB);
            // 绘制白色背景
            Graphics2D g2d = pageImage.createGraphics();
            g2d.setColor(Color.WHITE);
            g2d.fillRect(0, 0, pageImage.getWidth(), pageImage.getHeight());
            // 绘制当前分页到新的BufferedImage对象中
            g2d.drawImage(
                    originalImage,
                    0, 0, originalImage.getWidth(), pageHeight,
                    0, startY, originalImage.getWidth(), startY + pageHeight,
                    null);
            // 保存当前分页为单独的图像文件
            ImageIO.write(pageImage, "png",
                    new File(dest, (i + 1) + "-" + file.getName()));
        }
    }

    /**
     * 功能：下载图片到浏览器
     */
    public static void download(HttpServletResponse res, String imgName, String base64) throws Exception{
        OutputStream outputStream = res.getOutputStream();
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        String name = new String(imgName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
        // 设置一个响应头，无论是否被浏览器解析
        res.setHeader("Content-Disposition", "attachment;filename=" + name);
        res.setContentType("image/png");
        byte[] bytes = Base64.getDecoder().decode(base64);
        byteArrayOutputStream.write(bytes);
        byteArrayOutputStream.flush();
        byteArrayOutputStream.writeTo(outputStream);
        byteArrayOutputStream.flush();
        outputStream.flush();
        outputStream.close();
        byteArrayOutputStream.close();
    }


}
